<?php

namespace app\ow_test\controller;


use app\Request;

class Redenvelopes
{

    public function Redenvelopes()
    {

        //抢红包分为三步
        //1,设定红包的总金额
        //2，使用设置有多少个人抢红包
        //3，并且每个红包的金额加起来不大于总金额

        //mt_getrandmax() 等于 2147483647

        //动态调用方法
        $request = new \think\Request();

        if ($request->Post() || $request->isAjax()) {

            //红包
            $red = $request->post();

//            $total_price = 20;//红包的总金额
//            $num = 10;//抢红包的总人数
//            $min = 0.01;
//            $max = 20;//红包的最大金额，这里应该设置不能大于$total_price,另外这里红包最大数额很有问题，不应该加上的，以后写可以直接用红包的余额来进行代替
//            $overage = $total_price;//红包余额

            //var_dump($red);die;

            $total_price = $red["total_price"];
            $num = $red["num"];
            $max = $red["max"];
            $min = 0.01;
            $overage = $total_price;

            //键值为红包金额，键名为余额
            $result = $this->rand_red($min, $max, $total_price, $overage, $num);
            //var_dump($result);die;
            // var_dump(json_encode($result));
            // var_dump(json($result));

            // return json($result);

            return view("Redenvelopes/Redenvelopes", ['result' => $result]);

        } else {
            $request = [];
            return view("Redenvelopes/Redenvelopes", ['result' => $result ?? ""]);

        }


    }


    public function rand_red($min, $max, $total_price, $overage, $num)
    {
        //存储随机出来红包的数组
        $randoms = [];
        //存储余额的数组
        $overages = [];

        for ($i = 0; $i < $num; $i++) {
            $random = $this->rand_float($min, $max / $num * 2);

            //这里判断随机出来的金额是否比余额大，如果大的话则进入下一个判断
            if ($overage <= $random || ($i == $num - 1)) {

                //判断当余额小于随机金额时候循环是否到了最后一轮
                if ($i < $num - 1) {

                    //二倍均值法
                    $random = $this->rand_float($min, $overage / $num * 2);
                    $overage = bcsub($overage, $random, 2);
                    //echo "获得金额:" . $random . '&nbsp' . '&nbsp' . '&nbsp' . "余额:" . $overage . "<br>";
                    //存储到数组之中
                    $randoms[] = $random;
                    $overages[] = $overage;

                } else {
                    $random = $overage;
                    $overage = bcsub($overage, $random, 2);
                    //echo "获得金额:" . $random . '&nbsp' . '&nbsp' . '&nbsp' . "余额:" . $overage . "<br>";

                    //存储到数组之中
                    $randoms[] = $random;
                    $overages[] = $overage;
                    // var_dump($randoms,$overages);die;
                    $i = $num - 1;

                    $combine = array_combine($randoms, $overages);

                    return $combine;

                }

            } else {
                $overage = bcsub($overage, $random, 2);
                //echo "获得金额:" . $random . '&nbsp' . '&nbsp' . '&nbsp' . "余额:" . $overage . "<br>";

                //存储到数组之中
                $randoms[] = $random;
                $overages[] = $overage;
            }
        }
    }

    //随机数函数
    public function rand_float($min, $max)
    {
        $random = $min + mt_rand() / mt_getrandmax() * ($max - $min);
        $random = round($random, 2);
        return $random;
    }


}